﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace Base64Image
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
            string encode = "";
            //Encoding.UTF8.GetBytes();
            byte[] bytes = Encoding.GetEncoding(Encoding.UTF8).GetBytes(code);  //将一组字符编码为一个字节序列. 
            try
            {
                encode = Convert.ToBase64String(bytes);  //将8位无符号整数数组的子集转换为其等效的,以64为基的数字编码的字符串形式. 
            }
            catch
            {
               // encode = code;
            } 
             * */

            //byte[] bs= File.ReadAllBytes("b.gif");

            //string str = Convert.ToBase64String(bs);

            //WebClient wc = new WebClient();
            //byte[] bb = wc.DownloadData("http://www.greywyvern.com/img/site/head02.png");

            string filename = "How browsers work.htm";
            string fileContent = File.ReadAllText(filename);

            DirectoryInfo di = new DirectoryInfo("How browsers work_files");
            FileInfo[] fis = di.GetFiles();

            foreach (var item in fis)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("data:image/");
                sb.Append(item.Extension.Substring(1));
                sb.Append(";base64,");
                byte[] bs = File.ReadAllBytes(item.FullName);
                sb.Append(Convert.ToBase64String(bs)); ;

                string src = "./How browsers work_files/" + item.Name;

                fileContent = fileContent.Replace(src, sb.ToString());

            }
            

            File.WriteAllText("2" + filename, fileContent);


        }
    }
}
